<template>
  <div>
    <div style="margin-bottom: 15px">
      <el-input style="width: 200px" v-model="params.name" placeholder="请输入分类名称"></el-input>
      <el-button type="info" style="margin-left: 10px" plain @click="findBySearch()">查询</el-button>
      <el-button type="warning" style="margin-left: 10px" plain @click="reset()">清空</el-button>
      <el-button type="primary" style="margin-left: 10px" plain @click="add()" v-if="user.role==='管理员'">新增</el-button>
      <el-popconfirm title="确定删除这些数据吗？" @confirm="delBatch()">
        <el-button slot="reference" size="mini" type="danger" style="float: right;margin-left: 10px" v-if="user.role==='管理员'">批量删除</el-button>
      </el-popconfirm>
      <el-button slot="reference" size="mini" type="success" style="float: right;margin-left: 10px" plain @click="exp()" >导出分类</el-button>
      <el-upload
        action="http://localhost:8080/zcy/type/upload"
        style="float: right"
        :show-file-list="false"
        :on-success="successUpload">
      <el-button size="mini"v-if="user.role==='管理员'">点击上传</el-button>
    </el-upload>
    </div>
    <div>
      <el-table
          :data="tableData"
          stripe
          style="width: 100%"
          @selection-change="handleSelectionChange">
        <el-table-column
            type="selection"
            width="55"
        :reserve-selection="true"
            v-if="user.role==='管理员'">
        </el-table-column>
        <el-table-column
            prop="name"
            label="分类名称"
            width="140">
        </el-table-column>
        <el-table-column
            prop="description"
            label="分类描述"
            width="800">
        </el-table-column>
        <el-table-column label="操作" v-if="user.role==='管理员'">
          <template v-slot:default="scope">
            <el-button size="mini" @click="edit(scope.row)">编辑</el-button>
            <el-popconfirm title="确定删除吗？" @confirm="del(scope.row.id)">
              <el-button slot="reference" size="mini" type="danger" style="margin-left: 10px" >删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="block" style="margin-top: 15px">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="params.pageNum"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="params.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
    <div>
      <el-dialog title="请输入信息" :visible.sync="dialogFormVisible" width="30%">
        <el-form :model="form">
          <el-form-item label="分类名称" :label-width="formLabelWidth">
            <el-input v-model="form.name" autocomplete="off" style="width: 90%"></el-input>
          </el-form-item>
          <el-form-item label="分类描述" :label-width="formLabelWidth">
            <el-input v-model="form.description" autocomplete="off" style="width: 90%"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="submit()">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>

import request from "@/utils/request";

export default {
  data() {
    return {
      params:{
        name:'',
        phone:'',
        pageNum: 1,
        pageSize: 10
      },
      tableData: [],
      total: 0,
      dialogFormVisible: false,
      form:{},
      formLabelWidth:"20%",
      multipleSelection: [],
      user: localStorage.getItem("user")?JSON.parse(localStorage.getItem("user")):{}
    }
  },
  created() {
    this.findBySearch();
  },
  methods:{
    findBySearch(){
      request.get("/type/search",{
        params: this.params
      }).then(res => {
        if (res.code === '0'){
          this.tableData = res.data.list;
          this.total = res.data.total;
        }else{
          this.$message.error({
            message: res.msg,
            type: 'error'
          })
        }
      })
    },
    exp(){
      let user = localStorage.getItem("user");
      location.href='http://localhost:8080/zcy/type/export?token='+ JSON.parse(user).token;
    },
    successUpload(res) {
      if(res.code=='0'){
        this.$message({
          message: '成功',
          type: 'success'
        })
        this.findBySearch();
      }else{
        this.$message.error({
          message: res.msg,
          type: 'error'
        })
      }
    },
    delBatch(){
      if(this.multipleSelection.length==0){
        this.$message.warning("请选择要删除的数据!")
        return
      }
      request.put("/type/delBatch",this.multipleSelection).then(res=>{
        if(res.code==='0'){
          this.$message({
            message: '成功',
            type: 'success'
          })
          this.findBySearch();
        }else {
          this.$message.error({
            message: res.msg,
            type: 'error'
          })
        }
      })
    },
    handleSelectionChange(val){
      this.multipleSelection = val;
    },
    edit(obj){
      this.form=obj;
      this.dialogFormVisible=true;
    },
    add(){
      this.form= {};
      this.dialogFormVisible=true;
    },
    reset(){
      this.params = {
        name:'',
        phone:'',
        pageNum: 1,
        pageSize: 10
      }
      this.findBySearch();
    },
    handleSizeChange(pageSize){
      this.params.pageSize = pageSize;
      this.findBySearch();
    },
    handleCurrentChange(pageNum){
      this.params.pageNum = pageNum;
      this.findBySearch();
    },
    submit(){
      request.post("/type",this.form).then(res=>{
        if(res.code==='0'){
          this.$message({
            message: '成功',
            type: 'success'
          })
          this.dialogFormVisible=false;
          this.findBySearch();
        }else {
          this.$message.error({
            message: res.msg,
            type: 'error'
          })
        }
      })
    },
    del(id){
      request.delete("/type/"+id).then(res=>{
        if(res.code == '0'){
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.findBySearch();
        }else{
          this.$message.error({
            message: res.msg,
            type: 'error'
          })
        }
      })
    }
  }
}
</script>
